package com.b2b.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.b2b.model.BrowsingDto;
import com.b2b.model.common.ResponseBean;
import com.b2b.model.entity.Browsing;
import com.b2b.model.entity.goods.Goods;
import com.b2b.model.entity.goods.Stock;
import com.b2b.service.BrowsingService;
import com.b2b.service.goodsService.GoodsService;
import com.b2b.service.goodsService.StockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/browsing")
@PropertySource("classpath:config.properties")
public class BrowsingController {

    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;

    @Autowired
    BrowsingService browsingService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    StockService stockService;

    @PostMapping("/add")
    public ResponseBean add(@RequestBody BrowsingDto browsingDto){
        Browsing temp = browsingService.selectOne(browsingDto);
        if (temp != null) {
            browsingService.delete(temp);
            browsingDto.setBrowsingTimes(temp.getBrowsingTimes());
        }
        browsingService.insert(browsingDto);
        return new ResponseBean(HttpStatus.OK.value(), "新增成功(Insert Success)", null);
    }

    @DeleteMapping("/delete")
    public ResponseBean delete(@RequestParam Map<String, Object> map){
        BrowsingDto browsingDto = new BrowsingDto();
        browsingDto.setCompanyId(Integer.parseInt((String) map.get("companyId")));
        browsingDto.setGoodsId(Integer.parseInt((String) map.get("goodsId")));
        browsingService.delete(browsingDto);
        return new ResponseBean(HttpStatus.OK.value(), "删除成功(Delete Success)", null);
    }

    @GetMapping("/get")
    public ResponseBean get(@RequestParam Map<String, Object> map){
        int page = 1, rows = 10;
        if(map.containsKey("page"))
            page = Integer.parseInt(map.get("page").toString());
        if(map.containsKey("rows"))
            rows = Integer.parseInt(map.get("rows").toString());

        PageHelper.startPage(page, rows);
        Browsing browsing = new BrowsingDto();
        browsing.setCompanyId((Integer) map.get("companyId"));
        List<Browsing> browsingList = browsingService.select(browsing);
        List<BrowsingDto> browsingDtoList = new ArrayList<>();
        Goods goods;
        Stock stock = new Stock();
        BrowsingDto browsingDto = new BrowsingDto();
        for (Browsing temp: browsingList
             ) {
            goods = goodsService.selectByPrimaryKey(temp.getGoodsId());

            String goodsImgUrlListStr = goods.getGoodsImgUrlList();

            List<String> goodsImgUrlList = JSONObject.parseArray(goodsImgUrlListStr, String.class);

            browsingDto.setGoodsId(goods.getGoodsId());
            browsingDto.setGoodsName(goods.getGoodsName());
            browsingDto.setGoodsImgUrl(goodsImgUrlList.get(0));
            browsingDto.setBrowsingTime(temp.getBrowsingTime());
            stock.setGoodsId(goods.getGoodsId());
            browsingDto.setPrice(stockService.selectOne(stock).getPrice());
            browsingDtoList.add(browsingDto);
        }
        PageInfo<BrowsingDto> selectPage = new PageInfo<>(browsingDtoList);
        Map<String, Object> result = new HashMap<>();
        result.put("count", selectPage.getTotal());
        result.put("data", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }
}
